package com.mstore.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.mstore.action.ExchangeAction;
import com.mstore.pojo.ExchangeGood;
import com.mstore.pojo.ExchangeLog;

public class ExchangeGoodsDao extends SuperDao {

	private static Logger logger = Logger.getLogger(ExchangeAction.class
			.getName());

	/**
	 * 存储兑换商品
	 */
	public void addExgood(ExchangeGood exchangeGood) {
		super.save(exchangeGood);
	}

	public List<Object> getAllExgoods(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id = ? GROUP BY shop_id, goodname";
		Object[] values = new Object[] { exchangeGood.getEaId() };
		return super.getObject(hql, values);
	}

	public List<Object> getAllExgoodsDistinct(ExchangeGood exchangeGood) {
		String hql = " from ExchangeGood where ea_id = ? ";
		Object[] values = new Object[] { exchangeGood.getEaId() };
		return super.getObject(hql, values);
	}

	/**
	 * 兑换商品的总数
	 * 
	 * @param exchangeGood
	 * @return
	 */
	public List<Object> getAllExgoodsPage(ExchangeGood exchangeGood, int index) {
		/*
		 * String hql =
		 * "from ExchangeGood where ea_id = "+exchangeGood.getEaId()
		 * +" GROUP BY shop_id, goodname";
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeGood where ea_id =");
		sb.append(exchangeGood.getEaId());
		sb.append("GROUP BY shop_id, goodname");

		return super.findPage(index, sb.toString());
	}

	/**
	 * 通过企业id和shopid查询所有
	 * 
	 * @param exchangeGood
	 * @return
	 */
	public List<Object> getAllExgoodList(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id =? and shop_id=?";
		Object[] values = new Object[] { exchangeGood.getEaId(),
				exchangeGood.getShopId() };
		return super.getObject(hql, values);
	}

	/**
	 * 通过企业id和shopid分页查询
	 */
	public List<Object> getAllExgoodListPage(ExchangeGood exchangeGood,
			int index) {
		/*
		 * String hql =
		 * "from ExchangeLog where ea_id ="+exchangeGood.getEaId()+" and shop_id="
		 * +exchangeGood.getShopId()+" order by create_time desc"; return
		 * super.findPage(index, hql);
		 */
		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where ea_id =");
		sb.append(exchangeGood.getEaId());
		sb.append(" and shop_id=");
		sb.append(exchangeGood.getShopId());

		return super.find(sb.toString());

	}

	/**
	 * 通过企业id和shopid查询所有总数
	 */

	public List<Object> getAllExgoodListPageTotal(ExchangeGood exchangeGood) {
		/*
		 * String hql =
		 * "from ExchangeLog where ea_id ="+exchangeGood.getEaId()+" and shop_id="
		 * +exchangeGood.getShopId(); return super.find(hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where ea_id =");
		sb.append(exchangeGood.getEaId());
		sb.append(" and shop_id=");
		sb.append(exchangeGood.getShopId());
		return super.find(sb.toString());

	}

	/**
	 * 通过企业id和商品id
	 */
	public List<Object> getExgoodsPage(ExchangeGood exchangeGood, int index) {
		/*
		 * String hql =
		 * "from ExchangeGood where ea_id = "+exchangeGood.getEaId()
		 * +"  and   id='"+exchangeGood.getId()+"' GROUP BY shop_id, goodname";
		 * return super.findPage(index, hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeGood where ea_id = ");
		sb.append(exchangeGood.getEaId());
		sb.append("  and   id='");
		sb.append(exchangeGood.getId());
		sb.append("' GROUP BY shop_id, goodname");
		return super.findPage(index, sb.toString());
	}

	/**
	 * 通过企业id和商品id总页数
	 */
	public List<Object> getExgoodsPageTotal(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id = "
				+ exchangeGood.getEaId() + "  and   id='"
				+ exchangeGood.getId() + "' GROUP BY shop_id, goodname";
		return super.find(hql);

	}

	/**
	 * 通过企业id和商品id总页数
	 */
	public List<Object> getExgoodsPageTotal1(ExchangeGood exchangeGood) {
		/*
		 * String hql =
		 * "from ExchangeGood where ea_id = "+exchangeGood.getEaId()
		 * +"  and   goodname='"+exchangeGood.getGoodname()+"'"; return
		 * super.find(hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeGood where ea_id = ");
		sb.append(exchangeGood.getEaId());
		sb.append("  and   goodname='");
		sb.append(exchangeGood.getGoodname());
		sb.append(" ' ");
		return super.find(sb.toString());
	}

	/**
	 * 通过店铺id查询兑换商品信息
	 */
	public List<Object> getAllExgoodsByShopId(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id = ? and shop_id = ? order by create_time desc";
		Object[] values = new Object[] { exchangeGood.getEaId(),
				exchangeGood.getShopId() };
		return super.getObject(hql, values);
	}

	public Object getOneExgoodbyId(Long id) {
		String hql = "from ExchangeGood where id=?";
		List<Object> objects = super.getObject(hql, new Object[] { id });
		if (objects.size() != 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	public void updateExgoodamout(ExchangeGood exchangeGood) {
		/*
		 * String sql = "update ubcenter.b_exchangegood set amount = " +
		 * exchangeGood.getAmount() + " where id = " + exchangeGood.getId();
		 * super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.b_exchangegood set amount = ");
		sb.append(exchangeGood.getAmount());
		sb.append(" where id = ");
		sb.append(exchangeGood.getId());

		super.executeUpdate(sb.toString());
	}

	/**
	 * 通过商品名称、企业id、店铺id查询兑换商品信息
	 */
	public List<Object> getExgoodsByGoodname(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id = ? and shop_id = ? and id = ?";
		Object[] values = new Object[] { exchangeGood.getEaId(),
				exchangeGood.getShopId(), exchangeGood.getId() };
		return super.getObject(hql, values);
	}

	/**
	 * 通过商品名称、店铺id查询兑换商品信息
	 */
	public List<Object> getExgoodsByGoodnameAndShopId(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where shopId = ? and goodname = ?";
		Object[] values = new Object[] { exchangeGood.getShopId(),
				exchangeGood.getGoodname() };
		return super.getObject(hql, values);
	}

	/**
	 * 通过商品名称、企业id、店铺id查询兑换商品信息 分页
	 */
	public List<Object> getExgoodsByGoodidPage(ExchangeLog exchangeLog,
			int index) {
		logger.info("exchangeLogDao=" + exchangeLog);
		logger.info("indexDao=" + index);
		/*
		 * String hql = "from ExchangeLog where eaId = " + exchangeLog.getEaId()
		 * + " and shopId =" + exchangeLog.getShopId() + " and  goodId=" +
		 * exchangeLog.getGoodId() + "  order  by create_time desc";
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where eaId = ");
		sb.append(exchangeLog.getEaId());
		sb.append(" and shopId =");
		sb.append(exchangeLog.getShopId());
		sb.append(" and  goodId=");
		sb.append(exchangeLog.getGoodId());
		sb.append("  order  by create_time desc");
		logger.info(sb.toString());
		return super.findPage(index, sb.toString());
	}

	/**
	 * 通过商品名称、企业id、店铺id查询兑换商品信息 总数
	 */
	public List<Object> getExgoodsByGoodidPageTotal(ExchangeLog exchangeLog) {
		/*
		 * String hql = "from ExchangeLog where eaId = " + exchangeLog.getEaId()
		 * + " and shopId =" + exchangeLog.getShopId() + " and  goodId =" +
		 * exchangeLog.getGoodId(); return super.find(hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where eaId = ");
		sb.append(exchangeLog.getEaId());
		sb.append(" and shopId =");
		sb.append(exchangeLog.getShopId());
		sb.append(" and  goodId =");
		sb.append(exchangeLog.getGoodId());
		return super.find(sb.toString());
	}

	/**
	 * 通过商品名称、企业id、店铺id、排除自身id查询兑换商品信息
	 */
	public List<Object> getExgoodsByGoodnameAndId(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where ea_id = ? and shop_id = ? and goodname = ? and id != ?";
		Object[] values = new Object[] { exchangeGood.getEaId(),
				exchangeGood.getShopId(), exchangeGood.getGoodname(),
				exchangeGood.getId() };
		return super.getObject(hql, values);
	}

	/**
	 * 删除商品
	 */
	public void delExgood(ExchangeGood exchangeGood) {
		super.delete(exchangeGood);
	}

	/**
	 * 通过id查询兑换商品信息
	 */
	public List<Object> getExgoodById(ExchangeGood exchangeGood) {
		String hql = "from ExchangeGood where id = ?";
		Object[] values = new Object[] { exchangeGood.getId() };
		return super.getObject(hql, values);
	}

	/**
	 * 修改兑换商品信息
	 */
	public void updateExgood(ExchangeGood exchangeGood) {
		/*
		 * String sql = "update b_exchangegood set goodname = '" +
		 * exchangeGood.getGoodname() + "', shop_id = " +
		 * exchangeGood.getShopId() + ", score = " + exchangeGood.getScore() +
		 * ", amount = " + exchangeGood.getAmount() + " where id = " +
		 * exchangeGood.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update b_exchangegood set goodname = '");
		sb.append(exchangeGood.getGoodname());
		sb.append("', shop_id = ");
		sb.append(exchangeGood.getShopId());
		sb.append(", score = ");
		sb.append(exchangeGood.getScore());
		sb.append(", amount = ");
		sb.append(exchangeGood.getAmount());
		sb.append(", exchange_amount = ");
		sb.append(exchangeGood.getExchangeAmount());
		sb.append(" where id = ");
		sb.append(exchangeGood.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 查询兑换记录
	 */
	public List<Object> getAllExLog(ExchangeLog exchangeLog) {
		String hql = "from ExchangeLog where ea_id = ? order by create_time desc";
		Object[] values = new Object[] { exchangeLog.getEaId() };
		return super.getObject(hql, values);
	}

	/**
	 * 分页查询兑换记录
	 */
	public List<Object> getAllExLogPage(int index, ExchangeLog exchangeLog) {
		/*
		 * String hql = "from ExchangeLog where ea_id = " +
		 * exchangeLog.getEaId() + " order by create_time desc"; return
		 * super.findPage(index, hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where ea_id = ");
		sb.append(exchangeLog.getEaId());
		sb.append(" order by create_time desc");

		return super.findPage(index, sb.toString());
	}

	/**
	 * 查询兑换记录条数
	 */
	public int getExLogCount(ExchangeLog exchangeLog) {
		String sql = "SELECT COUNT(eg) FROM ExchangeLog eg WHERE eg.eaId = ?";
		Object[] values = new Object[] { exchangeLog.getEaId() };
		List<Object> objects = super.getObject(sql, values);
		Long count = (Long) objects.get(0);
		return count.intValue();
	}

	/**
	 * 通过会员id查询兑换记录的条数
	 */
	public int getExLogCountByMemberId(ExchangeLog exchangeLog) {
		String sql = "SELECT COUNT(eg) FROM ExchangeLog eg WHERE eg.eaId = ? AND eg.memberId = ?";
		Object[] values = new Object[] { exchangeLog.getEaId(),
				exchangeLog.getMemberId() };
		List<Object> objects = super.getObject(sql, values);
		Long count = (Long) objects.get(0);
		return count.intValue();
	}

	/**
	 * 通过会员id分页查询兑换记录
	 */
	public List<Object> getExLogPageByMemberId(int index,
			ExchangeLog exchangeLog) {
		/*
		 * String hql = "from ExchangeLog where ea_id = " +
		 * exchangeLog.getEaId() + " and member_id = " +
		 * exchangeLog.getMemberId() + " order by create_time desc"; return
		 * super.findPage(index, hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from ExchangeLog where ea_id = ");
		sb.append(exchangeLog.getEaId());
		sb.append(" and member_id = ");
		sb.append(exchangeLog.getMemberId());
		sb.append(" order by create_time desc");
		return super.findPage(index, sb.toString());
	}

	/**
	 * 通过条件查询兑换商品
	 * 分页
	 */
	public List<Object> getPageExgGoodsByCondition(int index,
			ExchangeGood exchangeGood) {
		StringBuilder sql = new StringBuilder();
		sql.append("from ExchangeGood");
		sql.append(createSearchSqlByCondition(exchangeGood));
		sql.append(" group by shopId");

		return findPage(index, sql.toString());
	}

	/**
	 * 通过条件计算兑换商品总数量
	 */
	public int getCountExgGoodsByCondition(ExchangeGood exchangeGood) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(id) from ExchangeGood");
		sql.append(createSearchSqlByCondition(exchangeGood));

		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return 0;
		}
		Long count = (Long) list.get(0);
		if (count == null) {
			return 0;
		}

		return count.intValue();
	}

	/**
	 * 根据条件组装sql语句
	 */
	private String createSearchSqlByCondition(ExchangeGood exchangeGood) {
		String separate = " where ";
		StringBuilder sql = new StringBuilder();

		// 主键
		Long id = exchangeGood.getId();
		if (id != null) {
			sql.append(separate);
			sql.append("id = ");
			sql.append(id);
			separate = " and ";
		}

		// 企业id
		Long eaId = exchangeGood.getEaId();
		if (eaId != null) {
			sql.append(separate);
			sql.append("eaId = ");
			sql.append(eaId);
			separate = " and ";
		}

		// 店铺id
		Long shopId = exchangeGood.getShopId();
		if (shopId != null && shopId != 0) {
			sql.append(separate);
			sql.append("shopId = ");
			sql.append(shopId);
			separate = " and ";
		}

		return sql.toString();
	}

}